<template>
  <ContainerGroup>
    <Container style="margin-bottom: 10px">
      <FilterForm
        ref="filterForm"
        :elements="elements"
        @submit="handleSearch"
        @reset="handleReset"
      >
        <Button icon="add" type="plain" @click="handleAdd">新增</Button>
      </FilterForm>
    </Container>
    <Container height="fill">
      <div class="wraper">
        <Table
          :columns="tableColumns"
          :loading="tableLoading"
          :data="tableData"
          :pagination="{
            total: total,
            current: currentPage,
            pageSize: pageSize,
          }"
          @paginationChange="pageChange"
        >
          <template #caozuo="{ row }">
            <Tooltip content="编辑" icon="edit" @click="handleEdit(row)" />
            <Tooltip content="删除" icon="delete" @click="handleDelete(row)" />
          </template>
        </Table>
      </div>
      <!-- 新增、编辑 -->
      <Drawer
        ref="drawerRef"
        :title="title"
        :visible.sync="showDrawer"
        width="420px"
        :footer-btns="[
          {
            text: '确认',
            type: 'primary',
            loading: qrLoading,
            trigger: 'submit',
          },
          {
            text: '取消',
            type: 'info',
            trigger: 'cancel',
          },
        ]"
        @submit="handleSubmit"
        @cancel="showDrawer = false"
      >
        <el-form
          v-if="showDrawer"
          ref="drawerForm"
          :model="drawerForm"
          :rules="drawerRules"
          label-width="120px"
          size="mini"
          class="demo-drawerForm"
        >
          <el-form-item label="编码：" prop="code">
            <el-input
              v-model.trim="drawerForm.code"
              placeholder="请输入编码"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item label="院区名称：" prop="name">
            <el-input
              v-model.trim="drawerForm.name"
              placeholder="请输入院区名称"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注：">
            <el-input
              v-model.trim="drawerForm.remark"
              type="textarea"
              :rows="5"
              placeholder="请输入"
              maxlength="50"
              clearable
            />
          </el-form-item>
        </el-form>
      </Drawer>
    </Container>
  </ContainerGroup>
</template>
<script>
import {
  listHospitalAreaByPage,
  saveOrUpdateHospitalArea,
  deleteHospitalArea
} from "@/api/yard";
export default {
  data() {
    return {
      qrLoading: false,
      btnType: 1,
      optionList: [],
      drawerForm: {
        code: "",
        remark: "",
        name: "",
      },
      drawerRules: {
        name: [{ required: true, message: "请输入院区名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
      },
      darawerLoading: false,
      showDrawer: false,
      title: "",
      tableColumns: [
        {
          title: "编码",
          dataIndex: "code",
        },
        {
          title: "院区名称",
          dataIndex: "name",
        },
        {
          title: "备注",
          dataIndex: "remark",
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
        },
        {
          title: "操作",
          dataIndex: "caozuo",
          type: "otherTag",
          width: 160,
        },
      ],
      total: 0,
      pageSize: 100,
      currentPage: 1,
      tableLoading: false,
      tableData: [],
      newCondition: {},
      elements: [
        {
          label: "",
          type: "input",
          key: "nameOrCode",
          value: "",
          placeholder: "请输入编码或名称查询",
          width: 200,
        },
      ],
      currentRow: {},
    };
  },
  created() {},
  methods: {
    // 新增、编辑--保存
    handleSubmit() {
      this.$refs["drawerForm"].validate((valid) => {
        if (valid) {
          if (/[\u4e00-\u9fa5]+/.test(this.drawerForm.code)) {
            this.$message.warning("编码不能包含中文");
            return;
          }
          const param = {
            ...this.drawerForm,
          };
          if (this.btnType === 2) {
            // 编辑
            param.id = this.currentRow?.id;
          }
          this.qrLoading = true;
          saveOrUpdateHospitalArea(param).then((res) => {
            this.qrLoading = false;
            if (res.code === 200) {
              this.getData();
              this.showDrawer = false;
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 查询
    handleSearch(value) {
      this.newCondition = value;
      this.currentPage = 1;
      this.getData();
    },
    // 重置
    handleReset(val) {
      this.handleSearch(val);
    },
    // 编辑
    handleEdit(row) {
      this.currentRow = row;
      this.btnType = 2;
      this.title = "编辑院区";
      this.handleSetForm(row);
      this.showDrawer = true;
    },
    handleSetForm(row) {
      this.$handleSetForm(this.drawerForm, 1, row);
    },
    // 删除
    handleDelete(row) {
      this.$ckConfirm({
        content: "此操作将删除该条记录，并无法恢复， 是否继续?",
        title: "提示",
        okFn: (res) => {
          deleteHospitalArea({
            id: row.id,
          }).then((res) => {
            if (res.code === 200) {
              this.currentPage = 1;
              this.getData();
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        },
        cancelFn: (res) => {
          console.log("取消");
        },
      });
    },
    // 新增
    handleAdd() {
      this.btnType = 1;
      this.title = "新增院区";
      this.$handleSetForm(this.drawerForm, 0);
      this.showDrawer = true;
    },
    // 数据列表
    getData() {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        nameOrCode: this.newCondition.nameOrCode,
      };
      this.tableLoading = true;
      listHospitalAreaByPage(params)
        .then((res) => {
          this.tableLoading = false;
          if (res.code === 200) {
            this.tableData = res.data.records;
            this.total = res.data.total;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    // 分页
    pageChange(payload) {
      this.currentPage = payload.current;
      this.pageSize = payload.pageSize;
      this.getData();
    },
  },
};
</script>
<style lang="less" scoped>
.wraper {
  display: flex;
  height: 100%;
  overflow: hidden;
  .upload-demo {
    .buttons {
      margin: 0px 5px;
      height: 27px;
      margin-top: -1px;
    }
  }
}
</style>
